package pro.shuangxi.common.utils;

import cn.hutool.core.util.URLUtil;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：mengshx
 * @date ：Created in 2022/6/25 10:30
 * @description：
 */
public class WebPageUtils {
    public static List<String> analysisLink(String url){
        Connection connect = Jsoup.connect(url);
        ArrayList<String> linkList = new ArrayList<>();
        try {
            URI host = URLUtil.getHost(new URL(url));
            String domain = host.toString();
            String relativePath=url.substring(0, url.lastIndexOf("/"));

            Document document = connect.get();
            Elements allElements = new Elements();
            Elements link = document.getElementsByTag("link");
            Elements script = document.getElementsByTag("script");
            Elements img = document.getElementsByTag("img");
            allElements.addAll(link);
            allElements.addAll(script);
            allElements.addAll(img);


            for (Element element : allElements) {
                String tempUrl="";
                if (StringUtils.isNotBlank(element.attr("src"))) {
                    tempUrl=element.attr("src");
                }
                if (StringUtils.isNotBlank(element.attr("href"))) {
                    tempUrl=element.attr("href");
                }

                if(tempUrl.startsWith("//")){
                    tempUrl = "http:" + tempUrl;
                } else if (tempUrl.startsWith("/")) {
                    tempUrl=domain+tempUrl;
                } else if(tempUrl.startsWith("http")){
                    tempUrl=tempUrl;
                }else if (StringUtils.isNotBlank(tempUrl.trim().replace("/", ""))) {
                    tempUrl=relativePath+"/"+tempUrl;
                }

                if (StringUtils.isNotBlank(tempUrl)) {
                    linkList.add(tempUrl);
                }
            }
            return linkList;

        } catch (IOException e) {
            e.printStackTrace();
        }
    return linkList;
    }

}
